* sensitivity to fuel assumptions


* controls options
local controls_lla0 "" // IFO LS i.dow // unrate i.dow i.q  // i.m i.q // built dwt loa draft beam power_kw USflag
local controls_lla1 IFO LS // unrate i.dow i.q  // i.m i.q // built dwt loa draft beam power_kw USflag
local controls_lla2 IFO LS unrate // i.m i.q // built dwt loa draft beam power_kw USflag
*local controls_lla3 IFO LS c.unrate##c.unrate // i.m i.q // built dwt loa draft beam power_kw USflag

local controls_ves built dwt loa draft beam power_kw USflag group_agg_i

local vce "cluster ves_id"

local rte_opts all_coast all_exits
local folder rd_regs

local eca_ys 2009 // 2011
local exposeds 1	
local folder rd_regs
local vestype "Container"	
local vestype2 : subinstr local vestype " " ""	

capture drop reg_samp


*** getting distributions of alphas and finding minimum alpha by dwt quintiles
*** only using vessels in period we study
preserve
capture gen cnt = 1 
collapse (first) alpha_cons alpha_power vesseltype_regstr `controls_ves' (count) cnt if (sample_RD_regs==1) & eca_ind<=2 , by(ves_id)
drop if cnt==1 // eliminating vessels that only appear once
*hist alpha_cons  
*reg alpha_cons i.built
*predict res , res
*scatter res dwt

* get deadweight quintiles
egen dwt_quint=xtile(dwt), n(5) by(vesseltype_regstr)

*graph box alpha_cons if vesseltype_regstr=="Tanker", over(dwt_quint)

* calculate "efficient" alpha in each dwt quintile
* using 5th percentile here
egen eff_alpha = pctile(alpha_cons) , p(5) by(vesseltype_regstr dwt_quint)
* getting mean alpha
egen mean_alpha = mean(alpha_cons) , by(vesseltype_regstr)

gen alpha_scale_mean = mean_alpha/alpha_cons
gen alpha_scale_eff = eff_alpha/alpha_cons

* getting alpha quintiles
egen quart_alpha = xtile(alpha_cons) , n(4) by(vesseltype_regstr)

keep ves_id alpha_scale_mean alpha_scale_eff dwt_quint eff_alpha mean_alpha quart_alpha
save ves_alpha_tmp , replace
restore

merge m:1 ves_id  using ves_alpha_tmp  
drop _merge


preserve 
* adjust fuel consumption for more efficient alphas
gen f_meanalpha = fmain_cons*alpha_scale_mean + faux
gen f_eca09_meanalpha = fmain_eca09_cons*alpha_scale_mean + faux_eca09
gen f_eca11_meanalpha = fmain_eca11_cons*alpha_scale_mean + faux_eca11

gen f_effalpha = fmain_cons*alpha_scale_eff + faux
gen f_eca09_effalpha = fmain_eca09_cons*alpha_scale_eff + faux_eca09
gen f_eca11_effalpha = fmain_eca11_cons*alpha_scale_eff + faux_eca11

*** renaming some variables for syntax reasons
gen f_eca09_cons_main = fmain_eca09_cons 
gen f_cons_main = fmain_cons

* costs 
gen cost_eca09_power = f_eca09_power * mean_LS + f_out09_power * mean_IFO 
gen cost_eca09_cons_main = fmain_eca09_cons * mean_LS + fmain_out09_cons  * mean_IFO 
gen cost_eca09_meanalpha = ( fmain_eca09_cons*alpha_scale_mean + faux_eca09 ) * mean_LS + ( fmain_out09_cons*alpha_scale_mean + faux_out09 )  * mean_IFO
gen cost_eca09_effalpha = ( fmain_eca09_cons*alpha_scale_eff + faux_eca09 ) * mean_LS + ( fmain_out09_cons*alpha_scale_eff + faux_out09 )  * mean_IFO

* damages 
** since alpha is scalar on fuel, it also just scales damages - but only damages from main engine
gen td_inm_eca09_cons_main = tdmain_inm_eca09_cons 
gen td_inm_eca09_meanalpha = tdmain_inm_eca09_cons*alpha_scale_mean  + tdaux_eca09
gen td_inm_eca09_effalpha = tdmain_inm_eca09_cons*alpha_scale_eff  + tdaux_eca09

foreach rte_opt of local rte_opts {

	foreach eca_y of local eca_ys {
		if `eca_y' == 2009 {
			local run t_eca09
			local cut eca1
			local y_tag "09"
			
			* set sample criteria
			local bw 150
			local donut 0 
			local ports `" inlist(port_agg,"SoCal","NoCal") "'
			
			* placebo
			*local run_p t_eca_d1
			*local cut_p cut_d1
			
			* 
			local run_drop26 t_eca09_pre26dropped
			
		}
		if `eca_y'==2011 {
			local run t_eca11 
			local cut eca2
			local y_tag "11"
			
			* set sample criteria
			local bw 150
			local donut 0
			local ports `" inlist(port_agg,"SoCal") "' // only socal 
			
			*placebo
			*local run_p t_eca_d2
			*local cut_p cut_d2
			
			local run_drop26 t_eca11
		}
		
		* default route fixed effects
		local rte_fe route_id
		if "`rte_opt'"=="all_coast" {
			local rte_cond 	`" `ports' & route_type=="Coastal" "' 
			local samp_lab "port-port"
		}
				
		if "`rte_opt'"=="all_exits" {
			local rte_cond 	`" `ports' & route_type=="EnterExit"  "' 
			* local rte_fe route_agg // collapsing exit routes
			local samp_lab "ent/exit"
		}	
			
		local fe_vesrte "i.`rte_fe'#i.ves_id"	
		local fe_rte "i.`rte_fe'"	
		local fe_oprte "i.`rte_fe'#i.operator_group_i"	
		
		local xvar "i.cut#c.`run'" 
		local xvar_rte "i.cut#c.`run'#i.`rte_fe'"
		local xvar_rte_quad "i.cut#(c.`run'##c.`run')#i.`rte_fe'"
		
		local xvar_rte_drop26 "i.cut#c.`run_drop26'#i.`rte_fe'"
		
		capture drop samp_ind
		gen samp_ind = (sample_RD_regs==1) & `rte_cond' & vesseltype_regstr=="`vestype'" 
		
				
		* list of y-variables	
		local yvars td_inm_eca`y_tag' cost_eca`y_tag' f_eca`y_tag' f     
		
		foreach y of local yvars {
			local apprep replace
			local out_file "`folder'/ll_robfuel_`y'_`rte_opt'_`vestype2'_eca`eca_y'.tex"	
			display "`out_file'"
			
			capture drop cut
			gen cut = `cut'
			local lbl : variable label `cut'
			label var cut `"`lbl'"' 
			
			* using consumption alpha
			label var `y'_cons "Consump"
			reghdfe `y'_cons c.cut `xvar_rte' `controls_lla1' if samp_ind==1 & ( `run'<=`bw' & `run'>=-`bw' )  ///
						& ( `run'<-`donut' | `run'>`donut' ) , absorb(`fe_vesrte') vce(`vce')
			outreg2 using `out_file', `apprep' lab keep( cut ) tex(frag) nonotes nocon /// 
					addstat( Vessels , e(N_clust))  /// 
					addtext( "Fuel $\alpha$ " , "Consump" , "Main Only" , "~" ) ctitle("") // Quad Route Trends, "~",
			local apprep append
			capture drop reg_samp
			gen reg_samp = e(sample) // saving sample
			
			* using power alpha
			label var `y'_power "Power"
			reghdfe `y'_power c.cut `xvar_rte' `controls_lla1' if samp_ind==1 & ( `run'<=`bw' & `run'>=-`bw' )  ///
						& ( `run'<-`donut' | `run'>`donut' ) , absorb(`fe_vesrte') vce(`vce')
			outreg2 using `out_file', `apprep' lab keep( cut ) tex(frag) nonotes nocon /// 
					addstat( Vessels , e(N_clust))  /// 
					addtext( "Fuel $\alpha$ " , "Power" ) ctitle("") // Quad Route Trends, "~",
			local apprep append
						
			* just main engine
			label var `y'_cons_main "Main Engine"
			reghdfe `y'_cons_main c.cut `xvar_rte' `controls_lla1' if samp_ind==1 & ( `run'<=`bw' & `run'>=-`bw' )  ///
						& ( `run'<-`donut' | `run'>`donut' ) , absorb(`fe_vesrte') vce(`vce')
			outreg2 using `out_file', `apprep' lab keep( cut ) tex(frag) nonotes nocon /// 
					addstat( Vessels , e(N_clust))  /// 
					addtext( "Fuel $\alpha$ " , "Consump" , "Main Only" , X ) ctitle("") // Quad Route Trends, "~",
			local apprep append
			
			* mean alpha
			label var `y'_meanalpha "Mean Effic."
			reghdfe `y'_meanalpha c.cut `xvar_rte' `controls_lla1' if samp_ind==1 & ( `run'<=`bw' & `run'>=-`bw' )  ///
						& ( `run'<-`donut' | `run'>`donut' ) , absorb(`fe_vesrte') vce(`vce')
			outreg2 using `out_file', `apprep' lab keep( cut ) tex(frag) nonotes nocon /// 
					addstat( Vessels , e(N_clust))  /// 
					addtext( "Fuel $\alpha$ " , "Mean" ) ctitle("") // Quad Route Trends, "~",
			local apprep append
						
			* efficient alpha
			label var `y'_effalpha "Most Effic."
			reghdfe `y'_effalpha c.cut `xvar_rte' `controls_lla1' if samp_ind==1 & ( `run'<=`bw' & `run'>=-`bw' )  ///
						& ( `run'<-`donut' | `run'>`donut' ) , absorb(`fe_vesrte') vce(`vce')
			outreg2 using `out_file', `apprep' lab keep( cut ) tex(frag) nonotes nocon /// 
					addstat( Vessels , e(N_clust))  /// 
					addtext( "Fuel $\alpha$ " , "5th Perc" )  ctitle("") // Quad Route Trends, "~",
			local apprep append
						
			/*			
			* highest fuel consuming vessels
			label var dist_eca2009 "Top Quart"
			reghdfe dist_eca2009 c.cut `xvar_rte' `controls_lla1' if samp_ind==1 & ( `run'<=`bw' & `run'>=-`bw' )  ///
						& ( `run'<-`donut' | `run'>`donut' ) & quart_alpha>=4 , absorb(`fe_vesrte') vce(`vce')
			outreg2 using `out_file', `apprep' lab keep( cut ) tex(frag) nonotes nocon /// 
					addstat( Vessels , e(N_clust))  /// 
					addtext(  ) ctitle("") // Quad Route Trends, "~",
			local apprep append
					
			* highest fuel consuming vessels
			*label var f`eca_opt'_cons "Bottom Quart"
			reghdfe dist_eca2009 c.cut `xvar_rte' `controls_lla1' if samp_ind==1 & ( `run'<=`bw' & `run'>=-`bw' )  ///
						& ( `run'<-`donut' | `run'>`donut' ) & quart_alpha==1 , absorb(`fe_vesrte') vce(`vce')
			outreg2 using `out_file', `apprep' lab keep( cut ) tex(frag) nonotes nocon /// 
					addstat( Vessels , e(N_clust))  /// 
					addtext(  ) ctitle("") // Quad Route Trends, "~",
			local apprep append
			*/
					
					
			drop reg_samp
		}	
		drop samp_ind
	}
}

restore

/*
	foreach exposed of local exposeds {
		
		* set yvariables
		if `exposed'==1 {
			*local yvars ldist_eca`eca_y' ldist lf_cons 
			local yvars dist_eca`eca_y' dist f_cons   // Change name of table below
		}
		else {
			*local yvars ldist lf_cons 
			local yvars dist f_cons // change name of table below
		}
*/
